package cn.itrip.biz.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripImage;
import cn.itrip.beans.pojo.ItripLabelDic;
import cn.itrip.beans.vo.ItripImageVO;
import cn.itrip.beans.vo.ItripLabelDicVO;
import cn.itrip.beans.vo.hotelroom.ItripHotelRoomVO;
import cn.itrip.beans.vo.hotelroom.SearchHotelRoomVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.ErrorCode;
import cn.itrip.dao.service.ItripHotelRoomService;
import cn.itrip.dao.service.ItripImageService;
import cn.itrip.dao.service.ItripLabelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Tequila
 **/
@RestController
@RequestMapping("/biz/api/hotelroom")
public class ItripHotelRoomController {

    @Autowired
    public ItripHotelRoomService itripHotelRoomService;
    @Autowired
    private ItripLabelService itripLabelService;
    @Autowired
    private ItripImageService itripImageService;

    @GetMapping("/getimg/{targetId}")
    public Dto getImg(@PathVariable Integer targetId){
        List<ItripImageVO> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        try {
            map.put("type",1);
            map.put("targetId",targetId);
            List<ItripImage> itripImageList = itripImageService.getImage(map);
            // 装入vo
            for (ItripImage itripImage : itripImageList) {
                ItripImageVO vo = new ItripImageVO();
                vo.setImgUrl(itripImage.getImgurl());
                vo.setPosition(itripImage.getPosition());
                list.add(vo);
            }
        } catch (BeansException e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常",ErrorCode.SYSTEM_ERROR);
        }
        return DtoUtil.returnDataSuccess(list);
    }

    @GetMapping("/queryhotelroombed")
    public Dto queryHotelRoombed(){
        List<ItripLabelDicVO> voList = new ArrayList<>();
        try {
            Map<String,Object> map = new HashMap<>();
            map.put("parentId",1);
            List<ItripLabelDic> itripLabelDicList = itripLabelService.queryHotelFeature(map);
            for (ItripLabelDic itripLabelDic : itripLabelDicList) {
                ItripLabelDicVO vo = new ItripLabelDicVO();
                BeanUtils.copyProperties(itripLabelDic,vo);
                voList.add(vo);
            }

        }catch (Exception e){
            return DtoUtil.returnFail("系统异常",ErrorCode.SYSTEM_ERROR);
        }
        return DtoUtil.returnDataSuccess(voList);
    }

    @PostMapping("/queryhotelroombyhotel")
    public Dto queryHotelRoomByHotel(@RequestBody SearchHotelRoomVO searchHotelRoomVO){
        List<List<ItripHotelRoomVO>> voListParent= new ArrayList<>();
        // 判空
        try {
            if (EmptyUtils.isNotEmpty(searchHotelRoomVO.getHotelId())){
                if (EmptyUtils.isNotEmpty(searchHotelRoomVO.getStartDate()) && EmptyUtils.isNotEmpty(searchHotelRoomVO.getEndDate())){
                    // 判断酒店时间的起始时间不能大于结束时间
                    if(searchHotelRoomVO.getStartDate().getTime() - searchHotelRoomVO.getEndDate().getTime() < 0){
                        // 13432432432 13432432433
                        List<ItripHotelRoomVO> voList =  itripHotelRoomService.queryHotelRoomByHotel(searchHotelRoomVO);
                        for (ItripHotelRoomVO itripHotelRoomVO : voList) {
                            List<ItripHotelRoomVO> volistChild = new ArrayList<>();
                            volistChild.add(itripHotelRoomVO);
                            voListParent.add(volistChild);
                        }
                        return DtoUtil.returnDataSuccess(voListParent);
                    }else{
                        return DtoUtil.returnFail("起始时间不能大于结束时间!", ErrorCode.DATE_ERROR);
                    }
                }else{
                    return DtoUtil.returnFail("请输入起始时间和结束时间!", ErrorCode.PARAM_ISNULL);
                }
            }else{
                return DtoUtil.returnFail("酒店id不能为空", ErrorCode.PARAM_ISNULL);
            }
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常",ErrorCode.SYSTEM_ERROR);
        }

    }
}
